// 页面路径：store/index.js 
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex); //vue的插件机制
import {
	HTTP_REQUEST_URL
} from "@/config/app.js"
import TencentCloudChat from '@tencentcloud/chat';
let chat = TencentCloudChat.create({
	SDKAppID: 1400818966 // Replace `0` with the `SDKAppID` of your Chat app during access.
});
//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state: { //存放状态
		version: "2.0.1",
		autoClose: false, //消息notification弹出是否自动关闭
		im: {
			msgCount: 0,
			chat: null
		},
		msgCount: {
			//消息通知中的系统消息
			system: 0,
			//消息通知中的交易订单状态功能变更通知
			other: 0
		},
		vipModalIsShow: {
			index: false,
			message: false
		},
		socketIsShow: {
			currentPage: '',
			message_list: []
		},
		tabBarItems: [],
		otherMessageCount: 0,
		systemMessageCount: 0,
		c2cMessageCount: 0,
		notificationRoute: "",
		workMessage: [],
		workRefresh: false
	},
	getters: {
		getChat: (state, getters) => {
			//state ：可以访问数据
			//getters：访问其他函数，等同于 store.getters
			return state.im.chat
		},
		imMsgCount: (state, getters) => {
			//state ：可以访问数据
			//getters：访问其他函数，等同于 store.getters
			return state.im.msgCount
		},
		sysMsgCount: (state, getters) => {
			//state ：可以访问数据
			//getters：访问其他函数，等同于 store.getters
			return state.msgCount?.system || 0
		},
		otherMsgCount: (state, getters) => {
			//state ：可以访问数据
			//getters：访问其他函数，等同于 store.getters
			return state.msgCount.other
		},
		getSocketState: (state, getters) => {
			//state ：可以访问数据
			//getters：访问其他函数，等同于 store.getters
			return state.socketIsShow
		},
	},
	mutations: {
		setSocketState(state, message_list) {
			let pages = getCurrentPages();
			let currentPage = pages[pages.length - 1];
			state.notificationRoute = currentPage.route
			state.socketIsShow.message_list = state.socketIsShow.message_list.concat(message_list);
		},
		setVipModalState(state, {
			key,
			value
		}) {
			state.vipModalIsShow[key] = value;
		},
		setImMsgCount(state, count) {
			// 变更状态
			state.im.msgCount = count
		},
		setImChat(state, chat) {
			state.im.chat = chat
		},
		setMsgCount(state, payload) {
			// console.log(payload);return false;
			// 变更状态
			state.msgCount = payload
		},
		setTabBarItems(state, list) {
			state.tabBarItems = list;
		},
		setMessageCount(state, count) {
			const tabBarItems = state.tabBarItems.map(item => {
				if (item.pagePath === '/pages/tabbar/message/index') {
					item.count = count;
				}
				return item;
			})
			if (count) {
				uni.setTabBarBadge({
					index: 3,
					text: String(count)
				})
			} else {
				uni.removeTabBarBadge({
					index: 3
				})
			}

			state.tabBarItems = tabBarItems;
		},
		setMessageCountAll(state, {
			otherMessageCount,
			systemMessageCount,
			c2cMessageCount
		}) {
			state.otherMessageCount = otherMessageCount
			state.systemMessageCount = systemMessageCount
			state.c2cMessageCount = c2cMessageCount
		},
		//获取各类消息总数
		getC2cUnreadMsgNum(state, groupNotReadMessage = 0) {
			const mobile = uni.getStorageSync('mobile')
			var that = this
			chat.on(TencentCloudChat.EVENT.TOTAL_UNREAD_MESSAGE_COUNT_UPDATED, function(event) {
				uni.setStorageSync("onTotalUnreadMessageCountUpdated", event.data)
				//是否在登入状态（看自己代码检测登入状态的判断方式）
				if (uni.getStorageSync('mobile')) {
					uni.request({
						url: HTTP_REQUEST_URL + '/tencentIm/getC2cUnreadMsgNum',
						data: {
							mobile: uni.getStorageSync('mobile')
						},
						header: {
							xzdToken: uni.getStorageSync("xzdToken")
						},
						success: (res) => {
							uni.request({
								url: HTTP_REQUEST_URL + '/basic/getNoticeNum',
								data: {
									mobile: uni.getStorageSync('mobile')
								},
								header: {
									xzdToken: uni.getStorageSync("xzdToken")
								},
								success: (r) => {
									var num = Number(r.data.data.other) +
										Number(r.data.data.system) + Number(uni
											.getStorageSync(
												"onTotalUnreadMessageCountUpdated"
											))
									console.log('_________________________' +
										num)
									if (num > 0) {
										// #ifdef APP-PLUS
										plus.runtime.setBadgeNumber(num)
										// #endif

										uni.setTabBarBadge({
											index: 3,
											text: String(num)
										})

										that.commit("setMessageCountAll", {
											otherMessageCount: Number(r
												.data.data.other),
											systemMessageCount: Number(r
												.data.data.system),
											c2cMessageCount: Number(uni
												.getStorageSync(
													"onTotalUnreadMessageCountUpdated"
												))
										})
										that.commit("setMessageCount", Number(
											uni.getStorageSync(
												"onTotalUnreadMessageCountUpdated"
											)) + Number(r.data.data
											.other) + Number(r.data.data
											.system))

									} else {
										uni.removeTabBarBadge({
											index: 3
										})
									}
								}
							})
						}
					})
				}
			})
		},
		setTabBar(state, vm) {
			//设置tabbaritem
			this.commit("setTabBarItems", [{
				iconPath: "/static/images/tabbar/newStyle/t1.png",
				selectedIconPath: "/static/images/tabbar/newStyle/t1s.png",
				text: vm.$t('首页'),
				// count: 2,
				// isDot: true,
				customIcon: false,
				pagePath: '/pages/tabbar/index'
			}, {
				iconPath: "/static/images/tabbar/newStyle/t2.png",
				selectedIconPath: "/static/images/tabbar/newStyle/t2s.png",
				text: vm.$t('订单'),
				customIcon: false,
				// isDot: true,
				pagePath: '/pages/tabbar/order/index'
			}, {
				iconPath: "/static/images/tabbar/newStyle/t3.png",
				selectedIconPath: "/static/images/tabbar/newStyle/t3s.png",
				text: vm.$t('发布'),
				// midButton: true,
				customIcon: false,
				pagePath: '/pages/tabbar/release/index'
			}, {
				iconPath: "/static/images/tabbar/newStyle/t4.png",
				selectedIconPath: "/static/images/tabbar/newStyle/t4s.png",
				text: vm.$t('消息'),
				count: 0,
				customIcon: false,
				pagePath: '/TUIKit/components/TUIConversation/list'
			}, {
				iconPath: "/static/images/tabbar/newStyle/t5.png",
				selectedIconPath: "/static/images/tabbar/newStyle/t5s.png",
				text: vm.$t('我的'),
				// count: 23,
				// isDot: false,
				customIcon: false,
				pagePath: '/pages/tabbar/user/index'
			}])
		},
		async getMyWorkMessage(state) {
			uni.request({
				url: HTTP_REQUEST_URL + "/aide/message_list",
				data: {
					user_id: uni.getStorageSync("user_id")
				},
				header: {
					xzdToken: uni.getStorageSync("xzdToken")
				},
				success: res => {
					state.workMessage = res.data.data
				}
			})
		},
		setWorkRefresh(state) {
			state.workRefresh = !state.workRefresh
		}
	}
})
export default store